package com.bps.zzant.json;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.bps.zzant.model.Customer;
import com.bps.zzant.services.CustomerService;


@Controller
public class CustomerJSON {
	
	private static final Logger logger = LoggerFactory.getLogger(CustomerJSON.class);
	
	@Autowired
	private CustomerService customerService;

	public void setCustomerService(CustomerService customerService) {
		this.customerService = customerService;
	}
	
	@RequestMapping(value = "getCustomerByName.json", method = RequestMethod.GET)
	public Map getCustomerByName(HttpServletRequest request, HttpServletResponse response){
		
		/*		Enumeration<String> a = request.getParameterNames();
	    for (; a.hasMoreElements(); ) {
	        logger.info("????????????"+(String)a.nextElement());
	    }	    
	    logger.info(" +++++++++in CustomerJSON.getCustomerByName. customerName:"+ request.getParameter("customerName"));
		logger.info(" ---------in CustomerJSON.getCustomerByName. term:"+ request.getParameter("term"));
	    */
		
	    Map<String, Object> map = new HashMap<String, Object>(); 
	    List<Customer> customerList;
		String customerName = request.getParameter("customerName");
		
		if(StringUtils.hasText(customerName)){
			customerList = customerService.getCustomersByName(customerName);
		}else{
			customerList = customerService.getAllCustomers();			
		}
		
		// logger.info(" +++++++++in CustomerJSON.getCustomerByName. customerList size:"+customerList.size());
		
		map.put("customerList", customerList);

		return map;
		
		
	}
	
	// the only difference of getCustomerByName.json and searchCustomerByName.json is the return object name: "customerList" or "rows"
	@RequestMapping(value = "searchCustomerByName.json", method = RequestMethod.GET)
	public Map searchCustomerByName(HttpServletRequest request, HttpServletResponse response){
	
	    Map<String, Object> map = new HashMap<String, Object>(); 
	    List<Customer> customerList;
		String customerName = request.getParameter("customerName");
		String phone1 = request.getParameter("phone1");
		
		// When I use "scrollview" in the datagrid to display large dataset quickly, search function using datagrid 'load' event will call URL twice, one with
		// "page" parameter which I don't need that call. So when I see it, just return with nothing
		if(request.getParameter("page")!=null){
			map.put("success", new Boolean("true"));
			return map;
		}
		
		logger.info(" +++++++++in CustomerJSON.getCustomerByName. Customer Name="+customerName+"| Phone1=" +phone1 +"|"+request.getParameter("page"));
		if(StringUtils.hasText(customerName)){
			customerList = customerService.getCustomersByName(customerName);
		}else if (StringUtils.hasText(phone1)){
			customerList = customerService.getCustomersByPhoneOrFax(phone1);
		}else{
			customerList = customerService.getAllCustomers();			
		}
		
		logger.info(" +++++++++in CustomerJSON.searchCustomerByName. customerList size:"+customerList.size());
				
		if(customerList.size()==0){
			// customerList = customerService.getAllCustomers();
			map.put("success", new Boolean("false"));
			
		}else{
			map.put("success", new Boolean("true"));
		}
		map.put("rows", customerList);
		map.put("total", customerList.size());
		
		return map;
		
		
	}
	
	@RequestMapping(value = "updateCustomer.json", method = RequestMethod.POST)
	public Map update(@Valid Customer customer, BindingResult errors){
		
/*		logger.info(" +++++++++in updateCustomer.json: id:"+ customer.getId());
		logger.info(" +++++++++in updateCustomer.json: customer name:"+ customer.getCustomerName());
		logger.info(" +++++++++in updateCustomer.json: address 1:"+ customer.getAddress1());
		logger.info(" +++++++++in updateCustomer.json: address 2:"+ customer.getAddress2());
		logger.info(" +++++++++in updateCustomer.json: city:"+ customer.getCity());
		logger.info(" +++++++++in updateCustomer.json: state:"+ customer.getState());
		logger.info(" +++++++++in updateCustomer.json: zip:"+ customer.getZip());
		logger.info(" +++++++++in updateCustomer.json: country:"+ customer.getCountry());
		
		logger.info(" +++++++++in updateCustomer.json: contact:"+ customer.getContact());
		logger.info(" +++++++++in updateCustomer.json: email:"+ customer.getEmail());
		logger.info(" +++++++++in updateCustomer.json: phone1:"+ customer.getPhone1());
		logger.info(" +++++++++in updateCustomer.json: phone2:"+ customer.getPhone2());
		logger.info(" +++++++++in updateCustomer.json: email:"+ customer.getEmail());
		logger.info(" +++++++++in updateCustomer.json: fax:"+ customer.getFax());
		
		logger.info(" +++++++++in updateCustomer.json: firstName:"+ customer.getFirstName());
		logger.info(" +++++++++in updateCustomer.json: lastName:"+ customer.getLastName());
		logger.info(" +++++++++in updateCustomer.json: midName:"+ customer.getMidName());
		logger.info(" +++++++++in updateCustomer.json: notes:"+ customer.getNotes());
		logger.info(" +++++++++in updateCustomer.json: resaleNum:"+ customer.getResaleNum());
		
		logger.info(" +++++++++in updateCustomer.json: salesRep:"+ customer.getSalesRep());
		logger.info(" +++++++++in updateCustomer.json: status:"+ customer.getStatus());
		logger.info(" +++++++++in updateCustomer.json: taxable:"+ customer.getTaxable());
		logger.info(" +++++++++in updateCustomer.json: terms:"+ customer.getTerms());
		*/
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("success", new Boolean("true"));
		
		try {
			customerService.updateCustomer(customer);
		} catch (Exception e) {
			map.put("success", new Boolean("false"));
		}
		
		return map;
	}
	
	@RequestMapping(value = "createCustomer.json", method = RequestMethod.POST)
	public Map createCustomer(@Valid Customer customer, BindingResult errors){
		
		logger.info(" +++++++++in updateCustomer.json:");
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("success", new Boolean("true"));
		
		try {
			customerService.saveCustomer(customer);
		} catch (Exception e) {
			map.put("success", new Boolean("false"));
		}
		
		return map;
	}
	
	
}
